<?php
include("database.php");
//require_once('class.phpgmailer.php');
require_once('class.phpmailer.php');
require_once('dataAccess.php');
class BaseClass extends MYSQL{
	private $email_from = "support@studysage.com.au";
	
	function isParentPage($id)
	{
		$qry = "select count(id) count from ".$this->PAGE." where parent_page = '".$id."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);

		$ret = ($data['count'] > 0)?1:0;
		
		return $ret;
	}
	
	function getPageId($page_name) {
		$qry = "select id,page_name from ".$this->PAGE." where link_name like '".$page_name."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return $data['id'];
	}
	
	function getCommonPageId($page_name) {
		$qry = "select id,page_name from ".$this->PAGE." where link_name like 'common'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return $data['id'];
	}
	
	function getTopBanner($id="") {
		$id = $this->getPageId($id);
		
		$qry = "SELECT upload_file, c.page_name
				FROM ".$this->BANNER_ADS." a
				INNER JOIN ".$this->MAP_BANNERS." b ON ( b.ad_name = a.id )
				INNER JOIN ".$this->PAGE." c ON ( b.page_name = c.id )
				WHERE c.id = '".$id."' AND banner_type LIKE 'top'
				AND a.active_status = '1'  AND b.active_status = '1'  
				AND c.active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		if($data['upload_file'] == "") {
			$id = $this->getCommonPageId($id);
			
			$qry = "SELECT upload_file, c.page_name
				FROM ".$this->BANNER_ADS." a
				INNER JOIN ".$this->MAP_BANNERS." b ON ( b.ad_name = a.id )
				INNER JOIN ".$this->PAGE." c ON ( b.page_name = c.id )
				WHERE c.id = '".$id."' AND banner_type LIKE 'top'
				AND a.active_status = '1'  AND b.active_status = '1'  
				AND c.active_status = '1'";
				
			$res = $this->exec_query($qry);
			$data = $this->fetch_row($res);
			
			$ret = $data['upload_file'];
		}
		else {
			$ret = $data['upload_file'];
		}
		return $ret;
	}
	
	function getRightBanner($id="") {
		$id = $this->getPageId($id);
		
		$qry = "SELECT upload_file,url,source
				FROM ".$this->BANNER_ADS." a
				INNER JOIN ".$this->MAP_BANNERS." b ON ( b.ad_name = a.id)
				INNER JOIN ".$this->PAGE." c ON ( b.page_name = c.id )
				WHERE c.id = '".$id."' AND banner_type LIKE 'right'
				AND a.active_status = '1'  AND b.active_status = '1'  
				AND c.active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);

		if(count($data) == 0) {
			$id = $this->getCommonPageId($id);
			
			$qry = "SELECT upload_file,url,source
				FROM ".$this->BANNER_ADS." a
				INNER JOIN ".$this->MAP_BANNERS." b ON ( b.ad_name = a.id )
				INNER JOIN ".$this->PAGE." c ON ( b.page_name = c.id )
				WHERE c.id = '".$id."' AND banner_type LIKE 'right'
				AND a.active_status = '1'  AND b.active_status = '1'  
				AND c.active_status = '1'";
			$res = $this->exec_query($qry);
			$data = $this->fetch_row_all($res);
			
			$ret = $data;
		}
		else {
			$ret = $data;
		}

		return $ret;
	}
	
	function drawFlashObject($file,$w="",$h="")
	{
					
		if($file != "") {
			$content .= 
				'<object id="presentation" width="'.$w.'" height="'.$h.'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" align="middle">
				<param name="allowScriptAccess" value="sameDomain" />
				<param name="movie" value="uploads/banners/'.$file.'" />
				<param name="quality" value="high" />
				<param name="bgcolor" value="#ffffff" />
				<param name="allowFullScreen" value="true" />
				
				<embed src="uploads/banners/'.$file.'" quality="high" bgcolor="#ffffff" width="'.$w.'" height="'.$h.'" name="presentation" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" allowFullScreen="true" />
				</object>';
		}
		else {
			$content = "";
		}
		
		return $content;
	}
	function video()
	{
					
		$qry = "select * from video";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		$h='212';
		$w='295';
		
		$file = $data['url'];
		
			$content .= 
				'<iframe title="YouTube video player" width="295" height="212" src="$file?rel=0" frameborder="0" allowfullscreen></iframe>';
		
		
		
		return $content;
	}
	function drawFlashObjectright($file,$w="",$h="",$url,$source)
	{
	   	echo $source;
		return $content;
	}
	
	function getStateName($id="") {
		$qry = "select state_name from ".$this->STATE." where state_id = '".$id."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return stripslashes($data['state_name']);
	}
	
	function getEventName($id="") {
		$qry = "select title from ".$this->MAINEVENTS." where id = '".$id."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return stripslashes($data['country_name']);
	}
	
	function getCountryName($id="") {
		$qry = "select country_name from ".$this->COUNTRY." where country_id = '".$id."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return stripslashes($data['country_name']);
	}	
	
	function getUserMailId($id)
	{
		$qry = "select email from ".$this->USER_QRY." where id = '".$id."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		return $data['email'];		
	}
	
	
	function getUserEmailId()
	{
		$qry = "SELECT  email, id FROM ".$this->USER_QRY."";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			$str .= '<option value="'.$data[$i]['email'].'">'.$data[$i]['email'].'</option>';
		}
		
		return $str;	
	}  
	
	function getNewsLetterUserEmailId()
	{
		$qry = "SELECT  email_id, id FROM ".$this->NEWSLETTER." WHERE active_status='1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);

		for($i=0;$i<count($data);$i++) {
			$str .= '<option value="'.$data[$i]['email_id'].'">'.$data[$i]['email_id'].'</option>';
		}
		
		return $str;	
	}	
	
	function generatePassword($length = 10)
	{
		// start with a blank password
		$password = "";
		
		// define possible characters
		$possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
		
		// set up a counter
		$i = 0; 
		
		// add random characters to $password until $length is reached
		while ($i < $length) { 
		
			// pick a random character from the possible ones
			$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
			
			// we don't want this character if it's already in the password
			if (!strstr($password, $char)) { 
				$password .= $char;
				$i++;
			}	
		}
	
		// done!
		return $password;	
	}
	
	function generate_userid($acc_type)
	{
		$qry = "select max(user_id) user_id from ".$this->USER." where user_id like '2%'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);

		if(is_null($data['user_id'])) {
			$new_userid = "20000001";
		}
		else {
			$new_userid = $data['user_id']+1;
		}
		
		return $new_userid;
	}
	
	function listPageName($sel_id="") 
	{
		$qry = "SELECT 	id,page_name FROM ".$this->PAGE_NAME." WHERE active_status = '1' ORDER BY page_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {

			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['page_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['page_name']).'</option>';
			}
		}
		
		return $str;
	}
	
	function listProfessions($sel_id="") {
		$qry = "SELECT id, title FROM ".$this->PROFESSION." 
				WHERE active_status = '1' ORDER BY title ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);

		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['title']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['title']).'</option>';
			}
			
			if($sel_id == "0") {
				$str .= '<option value="0" selected>Others</option>';
			}
			else {
				$str .= '<option value="0" >Others</option>';	
			}
		}
		
		return $str;
	}

	function listCountry($zone_id="",$sel_id="") {
		
		if($zone_id != "") {
			$where = " continent.zone_id = '".$zone_id."' and ";
		}
		
		$qry = "select continent.zone_id,country.country_name,country.country_id 
				from ".$this->CONTINENT."
				right join ".$this->COUNTRY." on continent.zone_id= country.zone_id 
				where ".$where."
				continent.active_status='1' and country.active_status='1' order by country.country_name ASC"; 
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['country_id']) {
				$str .= '<option value="'.$data[$i]['country_id'].'" selected>'.stripslashes($data[$i]['country_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['country_id'].'">'.stripslashes($data[$i]['country_name']).'</option>';
			}
		}
		
		return $str;
	}
	
	function listZone($sel_id="") {
		$qry = "SELECT zone_id, continent_name FROM ".$this->CONTINENT." WHERE active_status = '1' ORDER BY continent_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['zone_id']) {
				$str .= '<option value="'.$data[$i]['zone_id'].'" selected>'.stripslashes($data[$i]['continent_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['zone_id'].'">'.stripslashes($data[$i]['continent_name']).'</option>';
			}
		}
		
		return $str;
	}
	
	function listProgramZone($sel_id="") {
		$qry = "SELECT a.zone_id,`continent_name`
			  FROM ".$this->CONTINENT." a
			  INNER JOIN ".$this->PROGRAMS." b 
			  ON ( a.zone_id = b.zone_id )
			  GROUP BY continent_name";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['zone_id']) {
				$str .= '<option value="'.$data[$i]['zone_id'].'" selected>'.stripslashes($data[$i]['continent_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['zone_id'].'">'.stripslashes($data[$i]['continent_name']).'</option>';
			}
		}
		
		return $str;		
	}
	
function listcat($sel_id="") {
		$qry = "SELECT id, 	title FROM ".$this->ARTICLE_CAT." WHERE active_status = '1' ORDER BY title ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['title']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['title']).'</option>';
			}
		}
		
		return $str;
	}

	function listState($country_id,$sel_id="") {

		$qry = "SELECT state_id, state_name FROM ".$this->STATE." a 
				INNER JOIN ".$this->COUNTRY." b on (a.country_id = b.country_id) 
				WHERE a.country_id = '".$country_id."' AND a.active_status = '1'
				AND b.active_status = '1' ORDER BY state_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['state_id']) {
				$str .= '<option value="'.$data[$i]['state_id'].'" selected>'.stripslashes($data[$i]['state_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['state_id'].'">'.stripslashes($data[$i]['state_name']).'</option>';
			}
		}
		
		return $str;
	}

	function listUSState($country_id,$sel_id="") {

		$qry = "SELECT state_id, state_name FROM ".$this->STATE." a 
				INNER JOIN ".$this->COUNTRY." b on (a.country_id = b.country_id) 
				WHERE a.country_id = '".$country_id."' 
				AND (country_name like 'United States' OR country_name LIKE 'USA')
				AND a.active_status = '1'
				AND b.active_status = '1' ORDER BY state_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		$count = count($data);
		if($count > 0) {
			$str = "1~~";
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['state_id']) {
				$str .= '<option value="'.$data[$i]['state_id'].'" selected>'.stripslashes($data[$i]['state_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['state_id'].'">'.stripslashes($data[$i]['state_name']).'</option>';
			}
		}
		}
		else {
			$str = "0~~";
		}
		return $str;
	}
	
	function listUSCity($country_id,$state_id="",$sel_id="") {

		if($state_id != "") {
			$where_cond = " AND a.state_id = '".$state_id."' ";
		}
		
		$qry = "SELECT city_id, city_name 
				FROM ".$this->CITY." a
				INNER JOIN ".$this->COUNTRY." c ON ( a.`country_id` = c.`country_id` )
				LEFT JOIN ".$this->STATE." b ON ( a.`state_id` = b.`state_id` )
				WHERE a.country_id = '".$country_id."' 
				".$where_cond."
				AND (country_name like 'United States' OR country_name LIKE 'USA')
				AND a.active_status = '1'
				AND b.active_status = '1' ORDER BY city_name ASC";
				
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		$count = count($data);
		
		if($count > 0) {
			
			$str = "1~~";
			
			for($i=0;$i<count($data);$i++) {
				if($sel_id == $data[$i]['city_id']) {
					$str .= '<option value="'.$data[$i]['city_id'].'" selected>'.stripslashes($data[$i]['city_name']).'</option>';
				}
				else {
					$str .= '<option value="'.$data[$i]['city_id'].'">'.stripslashes($data[$i]['city_name']).'</option>';
				}
			}
		}
		else {
			$str = "0~~";
		}
		return $str;
	}	
	
	function listCity($country_id,$state_id="",$sel_id="") {

		if($state_id != "") {
			$where_cond = " AND a.state_id = '".$state_id."' ";
		}

		$qry = "SELECT city_id, city_name 
				FROM ".$this->CITY." a
				INNER JOIN ".$this->COUNTRY." c ON ( a.`country_id` = c.`country_id` )
				LEFT JOIN ".$this->STATE." b ON ( a.`state_id` = b.`state_id` )
				WHERE a.country_id = '".$country_id."' 
				".$where_cond."
				AND a.active_status = '1'
				AND b.active_status = '1' ORDER BY city_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		$count = count($data);

		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['city_id']) {
				$str .= '<option value="'.$data[$i]['city_id'].'" selected>'.stripslashes($data[$i]['city_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['city_id'].'">'.stripslashes($data[$i]['city_name']).'</option>';
			}
		}

		return $str;
	}
	
	function listCategory($sel_id="")
	{
		$qry = "SELECT id, category_name FROM ".$this->CATEGORY." 
				WHERE delete_status = '0' AND active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['category_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['category_name']).'</option>';
			}
		}
		
		return $str;		
	}

	function listSubCategory($category_id,$sel_id="")
	{
		$qry = "SELECT a.id, sub_cat_name FROM ".$this->SUB_CATEGORY." a INNER JOIN ".$this->CATEGORY." b ON (a.category_id = b.id) 
				WHERE a.category_id = '".$category_id."' 
				AND a.delete_status = '0' AND a.active_status = '1'
				AND b.delete_status = '0' AND b.active_status = '1'";
				
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['sub_cat_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['sub_cat_name']).'</option>';
			}
		}
		
		return $str;		
	}

	function includeItems($start,$end,$step_val="",$sel_id="")
	{
		if($step_val == "") {
			for($i=$start;$i<=$end;$i++) {
				if($sel_id == $i) {
					$content .= '<option value="'.$i.'" selected>'.$i.'</option>';
				}
				else {
					$content .= '<option value="'.$i.'">'.$i.'</option>';	
				}
			}
		}
		else {
			for($i=$start;$i<=$end;$i=$i+$step_val) {
				$content .= '<option value="'.$i.'">'.$i.'</option>';
			}
		}
		
		return $content;
	}
	
	function listMainPrograms($sel_id="")
	{
		$qry = "SELECT 	id, title FROM ".$this->MAINPROGRAMS." 
				WHERE active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['title']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['title']).'</option>';
			}
		}
		
		return $str;			
		
	}

	function listMainEvents($sel_id="")
	{
		$qry = "SELECT 	id, title FROM ".$this->MAINEVENTS." 
				WHERE active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['title']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['title']).'</option>';
			}
		}
		
		return $str;			
		
	}
	
	function listEvents($sel_id="")
	{
		$qry = "SELECT 	a.id,a.title,b.country_name FROM ".$this->EVENTS." a INNER JOIN
				".$this->COUNTRY." b ON (a.country_id = b.country_id)
				WHERE a.active_status = '1' AND b.active_status = '1'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			if($sel_id == $data[$i]['id']) {
				$str .= '<option value="'.$data[$i]['id'].'" selected>'.stripslashes($data[$i]['title'].' - '.$data[$i]['country_name']).'</option>';
			}
			else {
				$str .= '<option value="'.$data[$i]['id'].'">'.stripslashes($data[$i]['title'].' - '.$data[$i]['country_name']).'</option>';
			}
		}
		
		return $str;			
	}
	
	function createThumbNail($image_name,$w="",$h="")
	{
	
		if($w == "") {
			$w = 97;
		}
		
		if($h == "") {
			$h = 88;
		}
		
		$quality = 100;
		list($width, $height, $type, $attr) = getimagesize("../uploads/".$image_name);
		$thumb=imagecreatefromjpeg("../uploads/".$image_name);
		$thumb_p = imagecreatetruecolor($w, $h);
		imagecopyresampled($thumb_p, $thumb, 0, 0, 0, 0, $w, $h, $width, $height);
		imagejpeg($thumb_p, "../uploads/thumb/".$image_name, $quality);	
		unlink("../uploads/".$image_name);
	}
	
	function changeOnlineStatus($status)
	{
		$upd_array = array('online_status'=>$status);	
		$this->update_query($this->USER, $upd_array, " WHERE id = '".$_SESSION['user_id']."'") ;
	}
	
	/*
	$curpage - Current page to be selected
	$totalpages - Total Pages
	$num_rec - Number of Pages to be displayed
	$function - Javascript Function Name
	$param - Function Parameter with "|" seperated
	*/
	function pagination($curpage,$totalpages,$num_rec="",$function="",$param="")
	{
		if($totalpages > 1) {
		$function = ($function =="")? "loadpag":$function;
		
		$arr_param = explode("|",$param);
		
		//if($param != ''){
		
			for($i=0;$i<count($arr_param);$i++){
				$param_list .= "'".$arr_param[$i]."',";
			}
		//}
		$link = "";

		if($num_rec == ""){
			$num_rec = 4;
		}
		
		if($totalpages <  $num_rec){
			$num_rec = $totalpages;
			$minuvalue = $num_rec/2;
		}
		$start = 1;
		
		// midvalue calculation
		$midvalue = floor($num_rec/2);
		
		//loop index calculation
   	    //$temp_loop_index = $curpage - $midvalue;
		 $temp_loop_index = $curpage - 1;
		
		if($temp_loop_index<1){
			$loop_index = 1;
		}
		else{
			$loop_index = $temp_loop_index;
		}
		$loop_end = $loop_index + $num_rec;
		
		if($loop_end > $totalpages){
			$loop_end = $totalpages+1;
			$loop_index = $loop_end - $num_rec;
		}
		$link .= '<div class="pagination"> ';
		if($curpage > 1){
				$link .= '<a href="javascript:;" onclick="'. $function .'('.$param_list.($start).','.$totalpages.');">&laquo; First</a><a href="javascript:;" onclick="'. $function .'('.$param_list.($curpage-1).','.$totalpages.');">&laquo; Previous </a>';
		}
		
		for($i=$loop_index; $i<$loop_end; $i++){
			if($curpage == $i){
				$link .= '<a href="javascript:;" class="number current" onclick="'. $function .'('.$param_list.$i.','.$totalpages.');">'.$i.'</a> ';
			}
			else{
				$link .= '<a href="javascript:;" class="number" onclick="'. $function .'('.$param_list.$i.','.$totalpages.');">'.$i.'</a> ';
			}
		}
		if($curpage < $totalpages){
			$link .= '<a href="javascript:;" onclick="'. $function .'('.$param_list.($curpage+1).','.$totalpages.');">Next &raquo;</a> 
					  <a href="javascript:;" onclick="'. $function .'('.$param_list.$totalpages.','.$totalpages.');">Last &raquo;</a>';
		}
		$link .= '</div>';
			return $link;
		}
	}	


	function formateDate($db_date,$date_format,$sep="",$src_format) 
	{
		if($sep == "") {
			$sep = "-";
		}
		if($source_format == "yyyy".$sep."mm".$sep."dd")
			list($year,$month,$date) = explode($sep,$db_date);
		else if($source_format == "yyyy".$sep."mm".$sep."dd") 
			list($year,$month,$date) = explode($sep,$db_date);
		
		switch($date_format)
		{
			case "dd/mm/yyyy":
				$date = $date."/".$month."/".$year;
				break;
			case "mm/dd/yyyy";	
				$date = $month."/".$date."/".$year;
				break;
			default:
				$date = $month."-".$date."-".$year;
				break;
		}
		return $date;
	}


	function sendMailUser($email, $subject, $message, $file_name="", $file_content="") 
	{
		$email_message = $message;
		if($file_name = "") {
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= 'From: Adminstrator<'.$this->admin_mail . ">\r\n";
		}
		else {
		$fdata = chunk_split(base64_encode($file_content)); //Encode data into text form
		//Determine mime type
		$ext = explode('.', $file_name);
		$ext = $ext[1];
		
		$mime_type = "text/html";	
		$headers = "From: ".$this->admin_mail;
		
		$semi_rand = md5(time());
		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
		
		$headers .= "\nMIME-Version: 1.0\n" .
		"Content-Type: multipart/mixed;\n" .
		" boundary=\"{$mime_boundary}\"";
		
		$email_message .= "This is a multi-part message in MIME format.\n\n" .
		"--{$mime_boundary}\n" .
		"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
		"Content-Transfer-Encoding: 7bit\n\n" .
		$email_message . "\n\n";
		
		
		$fileatt_type = "text/html"; // File Type
		
		$email_message .= "--{$mime_boundary}\n" .
		"-: {$fileatt_type};\n" .
		" name=\"{$file_name}\"\n" .
		"Content-Disposition: attachment;\n" .
		" filename=\"{$file_name}\"\n" .
		"Content-Transfer-Encoding: base64\n\n" .
		$fdata . "\n\n" .
		"--{$mime_boundary}\n";		
		}
		
		$mail_ids = explode(",",$email);
		
		for($i=0;$i < count($mail_ids);$i++) {
			mail($mail_ids[$i],$subject,$email_message,$headers);
		}

	}
	
	function listGender($sel_id="")
	{
		$sel_male = "";
		$sel_female = "";
		
		if($sel_id == "Male") {
			$sel_male = "selected";
		}
		elseif($sel_id == "Female") {
			$sel_female = "selected";
		}
		
		$str .= '<option value="Male" '.$sel_male.'>Male</option>
				<option value="Female" '.$sel_female.'>Female</option>';
		
		return $str;
	}
	
	function listRace($sel_id="")
	{
		$sel_afr = "";
		$sel_oth = "";
		
		if($sel_id == "Black/African") {
			$sel_afr = "selected";
		}
		elseif($sel_id == "Others") {
			$sel_oth = "selected";
		}
				
		
		$str .= '<option value="Black/African" '.$sel_afr.'>Black/African</option>
				<option value="Others" '.$sel_oth.'>Others</option>';
		
		return $str;
	}
	
		
	function listMonths($sel_id="")
	{
		
		$jan = "";
		$feb = "";
		$mar = "";
		$apr = "";
		$may = "";
		$jun = "";
		$jul = "";
		$aug = "";
		$sep = "";
		$oct = "";
		$nov = "";
		$dec = "";

		if($sel_id == "January") {
			$jan = "selected";
		}
		else if($sel_id == "February") {
			$feb = "selected";
		}
		else if($sel_id == "March") {
			$mar = "selected";
		}
		else if($sel_id == "April") {
			$apr = "selected";
		}
		else if($sel_id == "May") {
			
			$may = "selected";
		}
		else if($sel_id == "June") {
			$jun = "selected";
		}
		else if($sel_id =="July") {
			$jul = "selected";
		}
		else if($sel_id =="August") {
			$aug = "selected";
		}
		else if($sel_id == "September") {
			$sep = "selected";
		}
		else if($sel_id == "October") {
			$oct = "selected";
		}
		else if($sel_id =="November") {
			$nov = "selected";
		}
		else if($sel_id == "December") {
			$dec = "selected";
		}
		$str = '<option value="January" '.$jan.'>January</option>
                <option value="February" '.$feb.'>February</option>
                <option value="March" '.$mar.'>March</option>
                <option value="April" '.$apr.'>April</option>
                <option value="May" '.$may.'>May</option>
                <option value="June" '.$jun.'>June</option>
                <option value="July" '.$jul.'>July</option>
				<option value="August" '.$aug.'>August</option>
				<option value="September" '.$sep.'>September</option>
				<option value="October" '.$oct.'>October</option>
				<option value="November" '.$nov.'>November</option>
				<option value="December"'.$dec.'>December</option>
				';		
		return $str;		
	}
	
	function listSize($sel_id)
	{
		$small = "";
		$medium = "";
		$large = "";
		$xl = "";
		$xxl = "";
		
		if($sel_id == "S") {
			$small = "selected";
		}
		elseif($sel_id == "M") {
			$medium = "selected";
		}
		elseif($sel_id == "L") {
			$large = "selected";
		}
		elseif($sel_id == "XL") {
			$xl = "selected";
		}
		elseif($sel_id == "XXL") {
			$xxl = "selected";
		}
		
		$str .= '<option value="S" '.$small.'>S</option>
				 <option value="M" '.$medium.'>M</option>
				 <option value="L" '.$large.'>L</option>
				 <option value="XL" '.$xl.'>XL</option>
				 <option value="XXL" '.$xxl.'>XXL</option>';
			
		return $str;	
	}
	/* newuser - original */
	/*function newuser($_post_info) {
		$user_array = array(
						
					 'FirstName'=>$_post_info['fname'],
					 'LastName'=>$_post_info['lname'],
					 'Email'=>$_post_info['email'],
					 'UserName'=>$_post_info['username'],
					 'Password'=>$_post_info['passwd'],
					 'PasswordRem'=>$_post_info['passwd_rem'],
					 'PhoneNo'=>$_post_info['mobile_num'],
					 'BusinessName'=>$_post_info['business_name'],
					 'age'=>$_post_info['age'],
					 'zipcode'=>$_post_info['zipcode'],
					 'parent_permission'=>$_post_info['parent_permission'],
					 'user_type'=>$_post_info['user_type'],
					 'CreatedDate'=>gmdate('Y-m-d H:i:s')
					 );
		$ret = $this->insert_query($this->NEWUSER, $user_array);
	}*/
	//------------------------------------
	
	function newuser($_post_info) {
		//check username is exist
		$res="select Email, Password, key_confirm from users where UserName='" . $this->replaceQuoteMySql($_post_info['username'])  . "' OR Email = '" . $this->replaceQuoteMySql($_post_info['email']) . "'";			
		$res = $this->exec_query($res);
		$val = mysql_num_rows($res);
		if ($val == 0)
		{	
			$user_array = array(
							
						 'FirstName'=>$_post_info['fname'],
						 'LastName'=>$_post_info['lname'],
						 'Email'=>$_post_info['email'],
						 'StudentEmail'=>$_post_info['studentemail'],
						 'UserName'=>$_post_info['username'],
						 'Password'=>$_post_info['passwd'],
						 'PasswordRem'=>$_post_info['passwd_rem'],
						 'PhoneNo'=>$_post_info['mobile_num'],
						 'BusinessName'=>$_post_info['business_name'],
						 'age'=>$_post_info['age'],
						 'zipcode'=>$_post_info['zipcode'],
						 'parent_permission'=>$_post_info['parent_permission'],
						 'user_type'=>$_post_info['user_type'],
						 'active_status'=> 0,
						 'key_confirm'=>md5(time()),
						 'CreatedDate'=>gmdate('Y-m-d H:i:s')
						 );
			$ret = $this->insert_query($this->NEWUSER, $user_array);
			return true;
		}
		else
		{
			return false;
		}		
	}
	function newuser_box($_post_info) {
		
		    $res="SELECT * FROM users where Email ='".$_post_info['email_val']."'";
		   
		   
		  
		   $res = $this->exec_query($res);
		     $val = mysql_num_rows($res);
			
			
			if ($val == 0)
			{
			$user_array_box = array(
						
					 'FirstName'=>$_post_info['fname'],
					 'Email'=>$_post_info['email_val'],
					 'StudentEmail'=>$_post_info['studentemail_val'],
					 'Password'=>$_post_info['passwd'],
					 'parent_permission'=>$_post_info['parent_permission'],
					 'user_type'=>$_post_info['user_type'],
					 'active_status'=> 0,
					 'key_confirm'=>md5(time()),
					 'CreatedDate'=>gmdate('Y-m-d H:i:s')
					 );
		$ret = $this->insert_query($this->NEWUSER, $user_array_box);
			}
			else
			{
				echo '<script>alert("User Already Exist Please Use Different E mail For New User")</script>';
				echo '<script>location.href=\'../index.php?page=register\'</script>';
			}
	}
	
	/* sendMail - original */
	function sendMail($email)
	{
			 $res="select Email from users where Email='".$email."' OR StudentEmail='".$email."' and active_status='1'";
			
			$res = $this->exec_query($res);
			$val = mysql_num_rows($res);
			if ($val==1)
			{
					$qry = "select FirstName,Email, StudentEmail,UserName,Password  from users where Email='".$email."' OR StudentEmail='".$email."'";
					$res = $this->exec_query($qry);
					$data = $this->fetch_row($res);
					$header="from:studysage.org";
					$header.="Cc: ".$data['StudentEmail']."";
					$to="".$data['Email'];
					$subject="Account Information";
					$msg='Your Password is :'.$data['Password'];
					@mail($to,$subject,$msg,$header);
					echo '<script>location.href=\'../index.php?page=forgot_password&_err=1\'</script>';
					
					
			}
			else
			{
				echo '<script>alert("There\'s no Details for Entered E mail")</script>';
				
			}
					
	}
	
	/* Thuong's sendMailConfirmLink */
	function sendMailConfirmLink($email)
	{
			$res="select Email, StudentEmail, FirstName, Password, key_confirm from users where Email='".$email."' and active_status='0'";
			
			$res = $this->exec_query($res);
			$val = mysql_num_rows($res);
			$data1 = $this->fetch_row($res);
			$confirm_url = "http://www.studysage.com.au/index.php?page=register&act=confirm_register&k=".$data1[key_confirm]."";
			//$confirm_url = "http://www.assignmentwizard.com/index.php?page=confirm_register&k=".$data1[key_confirm]."";
			if ($val==1)
			{
					$qry = "select FirstName,Email, StudentEmail,UserName,key_confirm,Password  from users where Email='".$email."'";
					$res = $this->exec_query($qry);
					$data = $this->fetch_row($res);
					$header  = 'MIME-Version: 1.0' . "\r\n";
					$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
					$header .= 'From: ' . $this->email_from . "\r\n"; 
					$to="".$data['Email'];
					$password = $data['Password'];
					$firstname = $data['FirstName'];
					$subject="Confirmation link from studysage";
					//$msg='Please click this link to finish and confirm your register at studysage.org : <a href="'.$confirm_url.'" target="_blank">'.$confirm_url.'</a>';
					//$msg.='<br /><br />Thank you';
					
					$msg = '<p style="color:#7030A0"><b>Dear ' . $firstname . ',</b></p>							
							<p>Thank you for registering at <a href="http://www.studysage.com.au" target="_blank">http://www.studysage.com.au</a></p>							
							<p>Your login details are as follows:</p>							
							<p style="color:#7030A0;font-weight:bold">User name : ' . $to . '
							<br>Password : ' . $password . '<p>
							<p>Please click on the link below to activate your account and start using the assignmentwizard features<br/>
							<a href="'.$confirm_url.'" target="_blank">' . $confirm_url . '</a></p>							
							<br/>
							Kind Regards,
							<br/>
							Support Team
							<br/><br/>
							http://www.studysage.com.au
							';					
							$sendMailSuccess = mail($to,$subject,$msg,$header);
							if ($sendMailSuccess){
								//echo "send mail success";exit();
							}
							else
							{
								//echo "send mail fail";exit();
							}
					//echo '<script>location.href=\'../index.php?page=register&_suc=1\'</script>';
					
					// for normal email
					$to2= $data['StudentEmail'];
					//$msgs='Please check your student email to get Register Confirmation link.';
					//$msgs.='<br /><br />Thank you';
					if (isset($to2))
					{
						mail($to2,$subject,$msg,$header);
					}
					
					header("location:../index.php?page=thanks&email=" . $to);
					//echo '<script>location.href=\'../index.php?page=register&_suc=1\'</script>';
			}
			else
			{
				echo '<script>alert("There\'s no Details for Entered Email")</script>';
				
			}
					
	}
	
	/* Thuong's confirmRegister */
	function confirmRegister($k) {
		$sql = "UPDATE users SET active_status='1' WHERE key_confirm='".$k."'";
		$res = $this->exec_query($sql);				
		$result = mysql_affected_rows();		
		if ($result)
		{
			$res="select * from users where key_confirm='". $k . "'";	
			$res = $this->exec_query($res);			
			$data = $this->fetch_row($res);
			$_SESSION['user_id'] = $data['Id'];
			//if(isset($_SESSION['add']) && $_SESSION['add'] == 1) {				
				// get title_reference id for updating composedocument
				$sql_tr = "SELECT * FROM tbl_references WHERE User='".session_id()."'";
				$rs = $this->exec_query($sql_tr);
				$dt_tr = $this->fetch_row($rs);				
				$sql = "UPDATE composedocument SET username='".$data['Id']."', title_reference='".$dt_tr['ID']."' WHERE username='".session_id()."'";				
				$result = $this->exec_query($sql);				
				$sql2 = "UPDATE tbl_references SET User='".$data['Id']."' WHERE User='".session_id()."'";
				$result2 = $this->exec_query($sql2);				
			//}			
			
			//save location login - vuong
			$email = $data["Email"];
			$ip_address = $_SERVER['REMOTE_ADDR'];
			
			$query = $this->exec_query("SELECT * FROM university_info WHERE ip_address='$ip_address'");
			$row = $this->fetch_row($query);
			$university_name = null;
			if ($row)
			{
				$university_name = $row['university_name'];
			}
			
			$dt=time();
			$session_id = session_id();
			$sql = "INSERT INTO history_login (university_name, ip_address, email, session_id, active, datetime) VALUES ('$university_name', '$ip_address', '$email', '$session_id', 1, '$dt')";				
			$result = $this->exec_query($sql);
			
			//update university for student if we found more university address more than or equals 3 times in history login
			$sql = "SELECT h.university_name, COUNT( h.university_name ) 
					FROM history_login h 
					INNER JOIN university_info u ON h.ip_address = u.ip_address 
					WHERE h.email =  '$email' 
					GROUP BY h.university_name 
					HAVING COUNT( h.university_name ) >=3 
					ORDER BY COUNT( h.university_name ) DESC";
			
			$query = $this->exec_query($sql);		
			$row = $this->fetch_row($query);
			
			$university_name = null;
			if ($row)
			{
				$university_name = $row['university_name'];
				$sql = "UPDATE users SET university_name = '$university_name' WHERE email = '$email'";
				$this->exec_query($sql);
			}			
			//END OF SAVE LOCATION - VUONG
			
			//-------------------------------
			$redirect_url = $_SESSION['url'];
			unset($_SESSION['url']);
		}
		return $result;
		//echo '<script>location.href=\'../index.php?page=login\'</script>';
		//echo '<script>location.href=\'index.php?page=login\'</script>'; // for localhost
	}
	//----------------------------
	
	function Login($email,$password,$id="")
	{
		
		$res="select * from users where Email='".$email."' and Password='".$password."' and active_status='1'";	
		$res = $this->exec_query($res);
		$val = mysql_num_rows($res);
		$data = $this->fetch_row($res);
		
		$_SESSION['user_id']=$data['Id'];
		//unset($_SESSION['user_id']);
		
		// get title_reference id for updating composedocument
		$sql_tr = "SELECT * FROM tbl_references WHERE User='".session_id()."'";
		$rs = $this->exec_query($sql_tr);
		$dt_tr = $this->fetch_row($rs);
		
		if ($val==1)
		{
				/* add citation to user when login */
				if(isset($_SESSION['add']) && $_SESSION['add'] == 1) {
				$sql = "UPDATE composedocument SET username='".$data['Id']."', title_reference='".$dt_tr['ID']."' WHERE username='".session_id()."'";
				$result = $this->exec_query($sql);
				$sql2 = "UPDATE tbl_references SET User='".$data['Id']."' WHERE User='".session_id()."'";
				$result2 = $this->exec_query($sql2);
				}
				//-------------------------------
				$redirect_url = $_SESSION['url'];
				unset($_SESSION['url']);
				
				//save location login - vuong
				
				$ip_address = $_SERVER['REMOTE_ADDR'];
				
				$query = $this->exec_query("SELECT * FROM university_info WHERE ip_address='$ip_address'");
				$row = $this->fetch_row($query);
				$university_name = null;
				if ($row)
				{
					$university_name = $row['university_name'];
				}
				
				$dt=time();
				$session_id = session_id();
				$sql = "INSERT INTO history_login (university_name, ip_address, email, session_id, active, datetime) VALUES ('$university_name', '$ip_address', '$email', '$session_id', 1, '$dt')";				
				$result = $this->exec_query($sql);
				
				//update university for student if we found more university address more than or equals 3 times in history login
				$sql = "SELECT h.university_name, COUNT( h.university_name ) 
						FROM history_login h 
						INNER JOIN university_info u ON h.ip_address = u.ip_address 
						WHERE h.email =  '$email' 
						GROUP BY h.university_name 
						HAVING COUNT( h.university_name ) >=3 
						ORDER BY COUNT( h.university_name ) DESC";
				
				$query = $this->exec_query($sql);		
				$row = $this->fetch_row($query);
				$university_name = null;
				if ($row)
				{
					$university_name = $row['university_name'];
					$sql = "UPDATE users SET university_name = '$university_name' WHERE email = '$email'";
					$this->exec_query($sql);
				}
				
				$da = new dataAccess();
				$da->clear_old_history_login();
				
				//END OF SAVE LOCATION - VUONG
			
				//if($redirect_url=='http://www.studysage.com.au/index.php?page=login')
				//{
					//echo '<script>location.href=\'../index.php\'</script>';
					header("location:../index.php?page=assignment_manager&_v=j");
				//}
				//else
				//{
					//echo '<script>location.href=\''.$redirect_url.'\'</script>';
					//header("location:$redirect_url");
				//}				
		}
		else
		{
			//echo '<script>location.href=\'../index.php?page=login&_err=1\'</script>';			
			header('location:../index.php?page=login&_err=1');
		}

	}
	
	function contact($contact_info) {
		
			$cont_array = array(
						
					 'email'=>$contact_info['email'],
					 'name'=>$contact_info['name'],
					 'company'=>$contact_info['company'],
					 'street_address'=>$contact_info['address'],
					 'city'=>$contact_info['city'],
					 'state'=>$contact_info['state'],
					 'zip_code'=>$contact_info['zip'],
					 'country'=>$contact_info['country'],
					 'phone'=>$contact_info['phone'],
					 'fax'=>$contact_info['fax'],
					 'comments'=>$contact_info['comments'],
					 'created_date'=>gmdate('Y-m-d H:i:s')
					 );
		$ret = $this->insert_query($this->CONTACT, $cont_array);
		$qry="select Email from administration";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .="from:" . $this->email_from;
		//$headers="From:".$contact_info['email'];
		 $msg = 
		"<h2>The new user would like to contact the Administrator</h2>".		
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Name: </span>". $contact_info['name']. "</p>".		
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Email: </span>" . $contact_info['email'] . "</p>" .		
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Company: </span>" . $contact_info['company'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Street address: </span>" . $contact_info['address'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>City: </span>" . $contact_info['city'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>State: </span>" . $contact_info['state'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Zip code: </span>" . $contact_info['zip'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Country: </span>" . $contact_info['country'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Phone: </span>" . $contact_info['phone'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Fax: </span>" . $contact_info['fax'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Comments: </span>" . $contact_info['comments'] . "</p>" .
		"<p><span style='display:inline-block;width:120px;font-weight:bold'>Date: </span>" . gmdate('Y-m-d H:i:s') . "</p>";
					 
		$to=$data['Email'];				
		mail($to,"Contact", $msg, $headers);
		
		/*
		$mail = new PHPMailer(); 
		$mail->IsSMTP(); // send via SMTP
		//IsSMTP(); // send via SMTP
		$mail->SMTPAuth = true; // turn on SMTP authentication
		$mail->Username = "noreply.studysage@gmail.com"; // SMTP username
		$mail->Password = "a!Ll%nM3wr)k[q"; // SMTP password
		$webmaster_email = "noreply.studysage@gmail.com"; //Reply to this email ID
		$email = $contact_info['email']; // Recipients email ID
		$name = $contact_info['name']; // Recipient's name
		$mail->From = $webmaster_email;
		$mail->FromName = "Studysage";
		$mail->AddAddress($email,$name);
		$mail->AddReplyTo($webmaster_email,"Webmaster");
		$mail->WordWrap = 50; // set word wrap
		//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
		$mail->IsHTML(true); // send as HTML
		$mail->Subject = "Contact";
		$mail->Body = $msg; //HTML Body
		$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
		try {
			if(!$mail->Send())
			{
			echo "Mailer Error: " . $mail->ErrorInfo;
			}
			else
			{
			echo "Message has been sent";
			}
		}
		catch(Exception $e)
		{
			
		}
		/*
		$mail = new PHPGMailer();
		$mail->IsSMTP();
		$mail->Username = 'noreply.studysage@gmail.com'; 
		$mail->Password = 'a!Ll%nM3wr)k[q';
		$mail->From = 'noreply.studysage@gmail.com'; 
		$mail->FromName = 'noreply.studysage@gmail.com';
		$mail->Subject = 'Contact';
		$mail->AddAddress('vuong.dinh.quoc@gmail.com');
		$mail->Body = $msg;
		$mail->Send();
		exit();
		*/
  }
  
  function sendHtmlMail()
  {
  	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

	// Additional headers
	$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
	$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
	$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
	$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
  }
  
  function getNewsHeadLines()
  {
	  $sql = "SELECT Id,title FROM ".$this->NEWS." WHERE Status = '1' order by Id desc";
	  $res = $this->exec_query($sql);
	  $data = $this->fetch_row_all($res);
	  
	  return $data;
  }
  
  function getClassifieidsCategoryName($id="") {
	  $sql = "SELECT id,category FROM ".$this->CLASSIFIEDS_CAT." 
	  		  WHERE active_status = '1' AND id='".$id."'";
	  $res = $this->exec_query($sql);
	  $data = $this->fetch_row($res);
	  
	  return $data['category'];
  }
  
  function getClassifieidsSubCategoryName($id="") {
	  $sql = "SELECT id,sub_cat_name FROM ".$this->CLASSIFIEDS_SUB_CAT." 
	  		  WHERE active_status = '1' AND id='".$id."'";
	  $res = $this->exec_query($sql);
	  $data = $this->fetch_row($res);
	  
	  return $data['sub_cat_name'];
  }
  
  function Apply_job($to,$subject,$message,$from,$file_name,$file_content)
  {
	       
			$email_message = $message;
			$fdata = chunk_split(base64_encode($file_content)); //Encode data into text form
			//Determine mime type
			$ext = explode('.', $file_name);
			$ext = $ext[1];
			
			$mime_type = "text/html";	
			$headers = "From:<".$from.">";
			
			$semi_rand = md5(time());
			$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
			
			$headers .= "\nMIME-Version: 1.0\n" .
			"Content-Type: multipart/mixed;\n" .
			" boundary=\"{$mime_boundary}\"";
			
			$email_message .= "This is a multi-part message in MIME format.\n\n" .
			"--{$mime_boundary}\n" .
			"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
			"Content-Transfer-Encoding: 7bit\n\n" .
			$email_message . "\n\n";
			
			$fileatt_type = "text/html"; // File Type
			
			$email_message .= "--{$mime_boundary}\n" .
			"-: {$fileatt_type};\n" .
			" name=\"{$file_name}\"\n" .
			"Content-Disposition: attachment;\n" .
			" filename=\"{$file_name}\"\n" .
			"Content-Transfer-Encoding: base64\n\n" .
			$fdata . "\n\n" .
			"--{$mime_boundary}\n";		
			 mail($to,$subject,$email_message,$headers);
			
		 
    }
		
	function listGadget($sel_id="")
	{
		
		$qry = "SELECT gadget_id,gadget_name from ".$this->GEDGET." WHERE active_status = '1' ORDER BY gadget_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			$str .= '<option value="'.$data[$i]['gadget_id'].'">'.$data[$i]['gadget_name'].'</option>';
		}
		
		return $str;			
		
	}
	function listarea($sel_id="")
	{
		
		$qry = "SELECT id,zone_name from ".$this->ZONE." WHERE active_status = '1' ORDER BY zone_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			$str .= '<option value="'.$data[$i]['id'].'">'.$data[$i]['zone_name'].'</option>';
		}
		
		return $str;			
		
	}
	function listJob($sel_id="")
	{
		
		$qry = "SELECT j.id,category,js.sub_cat_name from ".$this->JOB_CAT." j
		INNER JOIN ".$this->JOB_SUB_CAT." js ON 
		j.id=js.category_id	WHERE j.active_status = '1' and js.active_status = '1' ORDER BY category ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		
		for($i=0;$i<count($data);$i++) {
			
			$str .= '<option value="'.$data[$i]['id'].'">'.$data[$i]['sub_cat_name'].'</option>';
		}
		
		return $str;			
		
	}
	
	function listMake($sel_id="")
	{
		$qry = "SELECT make,id from ".$this->GEDGET_TECH." WHERE active_status = '1' GROUP BY make ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		for($i=0;$i<count($data);$i++) {
		$str .= '<option value="'.$data[$i]['make'].'">'.$data[$i]['make'].'</option>';
		}
		
		return $str;			
		
	}
	function listUniversities($sel_id="")
	{
		$qry = "SELECT university_name,id,url from ".$this->UNIVERSITY." WHERE active_status = '1' order BY university_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		for($i=0;$i<count($data);$i++) {
		$str .= '<option value="'.$data[$i]['url'].'">'.$data[$i]['university_name'].'</option>';
		}
		
		return $str;			
		
	}
	function listCourse($sel_id="")
	{
		$qry = "SELECT course_name,u.url from ".$this->COURSE." c inner join  ".$this->UNIVERSITY." u ON c.univer_id =u.id WHERE u.active_status  = '1' and c.active_status='1' order BY course_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		for($i=0;$i<count($data);$i++) {
		$str .= '<option value="'.$data[$i]['url'].'">'.$data[$i]['course_name'].'</option>';
		}
		
		return $str;			
		
	}
	function listScholarname($sel_id="")
	{
        $qry = "SELECT u.url,s.scholarship_name FROM university u inner join scholarships s on s.university_id=u.id
		WHERE s.active_status  = '1' and u.active_status='1' order BY scholarship_name ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		for($i=0;$i<count($data);$i++) {
		$str .= '<option value="'.$data[$i]['url'].'">'.$data[$i]['scholarship_name'].'</option>';
		}
		
		return $str;			
		
	}
	function get_newscat()
	{
		$qry = "SELECT Category from ".$this->NEWS." WHERE 	Status = '1' GROUP BY Category ASC";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row_all($res);
		for($i=1;$i<count($data);$i++) {
		$str .= '<option value="'.$data[$i]['Category'].'">'.$data[$i]['Category'].'</option>';
		}
		
		return $str;			
		
	}
	function sell_product($_post_info,$file)
	{
	   if($_SESSION['user_id']!="")
		{
			$product_array = array(
						
					 'user_id'=>$_SESSION['user_id'],
					 'gadget'=>$_post_info['gadget'],
					 'make'=>$_post_info['make'],
					 'specifications'=>$_post_info['spec'],
					 'sale_flag'=>$_post_info['Quality'],
					 'price'=>$_post_info['price'],
					 'image'=>$file,
					 'created_datetime'=>gmdate('Y-m-d H:i:s'),
					 'active_status'=>0
					 );
		$ret = $this->insert_query($this->GEDGET_TECH, $product_array);
		$qry = "select * from ".$this->NEWUSER." where Id = '".$_SESSION['user_id']."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		$headers="From:info@studysage.com";
		 $msg = 'Welcome to our website!'."\n\n".
		'Congratulation Your product Succesfully posted please click the link to activate the product'. "\r\n". 
			'http://'.$_SERVER['HTTP_HOST'].'/index.php?page=logon&product=2&product_id='.$_post_info['gadget']
			."\n\n\n\n\n".'Regards,'."\n". 'Studysage Team';	
			
		
		$to=$data['Email'];
		mail($to,"Product Activation",$msg,$headers);
		
				}
		else
		{
			echo '<script>location.href=\'../index.php?page=register\'</script>';
		}
	}
	function book($_post_info,$file)
	{
	/*echo "<pre>";
	print_r($_post_info);
	echo "</pre>";*/
	if($_SESSION['user_id']!="")
		{
			$book_array = array(
						
					 'user_id'=>$_SESSION['user_id'],
					 'book_name'=>$_post_info['tit'],
					 'author'=>$_post_info['author'],
					 'edition'=>$_post_info['edi'],
					 'book_desc'=>mysql_real_escape_string($_post_info['desc']),
					 'ISBN'=>$_post_info['isbn'],
					 'price'=>$_post_info['price'],
					 'cover_image'=>$file,
					 'created_datetime'=>gmdate('Y-m-d H:i:s'),
					 'active_status'=>0
					 );
		$ret = $this->insert_query($this->BOOKS, $book_array);	
		$get_id="select max(id) id from books where user_id='".$_SESSION['user_id']."' and active_status='0'";
	    $res_id = $this->exec_query($get_id);
		$data_id = $this->fetch_row($res_id);
		$code=base64_encode($data_id['id']);
		
		$qry = "select * from ".$this->NEWUSER." where Id = '".$_SESSION['user_id']."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		
		$headers="From:info@studysage.com";
		 $msg = 'Welcome to our website!'."\n\n".
		'Congratulation Your Details Succesfully posted please click the link to activate the Approve the Posting'. "\r\n". 
			'http://'.$_SERVER['HTTP_HOST'].'/index.php?page=logon&_book=2&g_id='.$code."\n\n\n\n\n".'Regards,'."\n". 'Studysage Team';
			
		
		$to=$data['Email'];
	
		mail($to,"Vote Activation",$msg,$headers);
		echo '<script>location.href=\'../index.php?page=logon&_book=1\'</script>';
	
		}
		
		else
		{
			echo '<script>location.href=\'../index.php?page=register\'</script>';
		}
	}
	function Rate_univ($_post_info)
	{
	    
		 $sql="select * from ".$this->RATTING." where user_id='".$_SESSION['user_id']."'
		and search_id ='".$_post_info['univ_id']."' and active_status='1'";
		$res = $this->exec_query($sql);
		
		if(mysql_affected_rows())
	   {
		    echo '<script>location.href=\'../index.php?page=logon&rate_univ=1\'</script>';
		
	   }
	   else
	   {
		  
		$qry = "select Email from ".$this->NEWUSER." where Id='".$_SESSION['user_id']."'";
		$res = $this->exec_query($qry);
		$data = $this->fetch_row($res);
		
		$book_array = array(
					 'user_id'=>$_SESSION['user_id'],
					 'vote'=>1,
					 'search_id'=>$_post_info['univ_id'],
					 'active_status'=>0
					 );
		
		$del="delete from ".$this->RATTING." where user_id='".$_SESSION['user_id']."'
		and search_id ='".$_post_info['univ_id']."' and active_status='0'";
		mysql_query($del);
		$ret = $this->insert_query($this->RATTING, $book_array);
		$code=base64_encode($_post_info['univ_id']);
		$headers="From:info@studysage.com";
		 $msg = 'Welcome to our website!'."\n\n".
		'Congratulation Your Vote Succesfully posted please click the link to activate the Approve the vote'. "\r\n". 
			'http://'.$_SERVER['HTTP_HOST'].'/index.php?page=logon&rate_univ=3&vote_id='.$code."\n\n\n\n\n".'Regards,'."\n". 'Studysage Team';
			
		
		$to=$data['Email'];
	
		mail($to,"Vote Activation",$msg,$headers);
		echo '<script>location.href=\'../index.php?page=logon&rate_univ=2\'</script>';
		}
	}
 	
	function replaceQuoteMySql($text)
	{
		return str_replace("'", "''", $text);
	}
	
	public function sendEmailToFriend($to)
	{
		if ($_SESSION['user_id'])
		{
			$id = $_SESSION['user_id'];
			$qry = "select FirstName,LastName, Email from users where Id='".$id."'";
			$res = $this->exec_query($qry);
			$data = $this->fetch_row($res);
			$header  = 'MIME-Version: 1.0' . "\r\n";
			$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			$header .= 'From: ' . $this->email_from . "\r\n"; 
					
			$email = $data['Email'];
			$name = trim($data['FirstName'] . " " . $data['LastName']);				
			
			$subject="$name has invited you to join studysage";				
			
			$msg = '<p style="color:#7030A0"><b>Hi,</b></p>							
					<p>I am ' . $name . '. My email address is ' . $email . '.</p>
					<p>I\'ve been using Studysage.com.au and thought you might like to try it out. Here\'s an invitation to create an account. </p>
					<p>You can click <a href="http://studysage.com.au/index.php?page=register" target="_blank">here</a>, or copy and paste the link below to register</p>							
					<p style="color:gray">http://studysage.com.au/index.php?page=register</p>
					<br/>
					Kind Regards,
					<br/>
					Support Team
					<br/><br/>
					http://www.studysage.com.au
					';					
					$sendMailSuccess = mail($to,$subject,$msg,$header);									
		}
		else
		{
			echo "Please to login to use this function";
		}
	}
}
?>